#include "MyCustomThread.h"
#include "CM_ResultCollection.h"
#include <WMI_Process.h>

MyCustomThread::MyCustomThread(ResultFormatter& resFormatter, ADCM_Computer& adcmComputer, wstring& user,
							wstring& password, wstring& domain,bool isLocal)
							: _resFormatter(resFormatter), _adcmComputer(adcmComputer), _user(user),	_password(password),
							_domain(domain), _isLocal(isLocal), _exit(false)
{
}

DWORD MyCustomThread::Run(LPVOID arg)
{

	CM_ResultCollection *cmResult = new CM_ResultCollection();

	WMI_HResult whres;

	do
	{
		whres = bridget_communicator::getComputersResult(_adcmComputer.getIdComputer(),
		cmResult, _user, _password, wstring(_adcmComputer.getCn()), _domain, _isLocal);

		if (FAILED(whres.getHRESULT	()))
		{
			wcout << "Error in getcomputerResult method" << endl;
			break;		
		}
		
		_resFormatter.printResult(_adcmComputer, *cmResult);

		//vector<WMI_Process*> vecProcess = cmResult->getProcesses().getObjects();

		//vector<WMI_Process*>::iterator iter;

		//for (iter = vecProcess.begin(); iter != vecProcess.end(); iter ++)
		//{
		//	wcout << "Name: " << (*iter)->getCaption() << endl;
		//}

		// cout << "Esta en el while" << endl;
		_exit=true;

	} while (!_exit);


	return NULL;
}
